class Cart{
		constructor() {
		    this.eventBind();
		}
		
		
		getGoodsNumAndUpdate(){
			let oGoodsNum = document.getElementsByClassName("goods-num");
			let oGoodsTotalNum = document.getElementsByClassName("goods-total-num")[0];
			let goodsTotalNum = 0;
			
			for(let i=0; i<oGoodsNum.length; i++){
				goodsTotalNum += Number(oGoodsNum[i].innerHTML);
			}
			
			oGoodsTotalNum.innerHTML = goodsTotalNum;
		}
		
		
		getGoodsPriceAndUpdate(){
			let oGoodsSinglePrice = document.getElementsByClassName("goods-single-price");
			let oGoodsTotalPrice = document.getElementsByClassName("goods-total-price")[0];
			let goodsTotalPrice = 0;
			
			for(let i=0; i<oGoodsSinglePrice.length; i++){
				goodsTotalPrice += Number(oGoodsSinglePrice[i].innerHTML);
			}
			
			oGoodsTotalPrice.innerHTML = goodsTotalPrice;
		}
		
		
		getSinglePrice(num,price){
			return num * price;
		}
		
		
		addGoods(btn){
			let oGoodsNum = btn.previousElementSibling;
			
			oGoodsNum.innerHTML = Number(oGoodsNum.innerHTML) + 1;
			
			
			let oGoodsUnitPrice = btn.parentNode.nextElementSibling.firstElementChild;
			let oGoodsSinglePrice = btn.parentNode.nextElementSibling.nextElementSibling.firstElementChild;
			
			oGoodsSinglePrice.innerHTML = this.getSinglePrice(oGoodsNum.innerHTML,oGoodsUnitPrice.innerHTML);
			
			
			this.getGoodsNumAndUpdate();
			
			this.getGoodsPriceAndUpdate();
		}
		
		minGoods(btn){
			let oGoodsNum = btn.nextElementSibling;
			
			if(oGoodsNum.innerHTML>0){
				oGoodsNum.innerHTML = Number(oGoodsNum.innerHTML) - 1;
				let oGoodsUnitPrice = btn.parentNode.nextElementSibling.firstElementChild;
				let oGoodsSinglePrice = btn.parentNode.nextElementSibling.nextElementSibling.firstElementChild;
				
				oGoodsSinglePrice.innerHTML = this.getSinglePrice(oGoodsNum.innerHTML,oGoodsUnitPrice.innerHTML);
				
				
				this.getGoodsNumAndUpdate();
				
				this.getGoodsPriceAndUpdate();
			}
		}
		
		delGoods(btn){
			let oTr = btn.parentNode.parentNode;
			
			oTr.remove();
			
			this.getGoodsNumAndUpdate();
			
			this.getGoodsPriceAndUpdate();
		}
		
		eventBind(){
			let oBtns = document.getElementsByTagName("button");
			let that = this;
			for(let i=0; i<oBtns.length; i++){
				if(i%2==0){
					oBtns[i].onclick = function(){
						that.minGoods(this);
					}
				}else{
					oBtns[i].onclick = function(){
						that.addGoods(this);
					}
				}
			}
			
			let oDelBtns = document.getElementsByTagName("input");
			
			for(let i=0; i<oDelBtns.length; i++){
				oDelBtns[i].onclick = function(){
					that.delGoods(this);
				}
			}
		}
	}
	
	let c = new Cart();